<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="iwpl.project.entities.*"%>
<%@page import="iwpl.project.servlet.*"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="iwpl.project.dao.CustomersDAO"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
//String t = (String)session.getAttribute("Manage_Customer");
//if(t!="active")
	//{response.sendRedirect("index.jsp");}
 %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="css/CommonCSS.css" type="text/css"></link>
</head>
<body>
	<div>
			<div style="margin: 0 auto; margin-top: 2px; margin-bottom: 0px">
			<form action="" method="post">
				<table style="margin: 0 auto; width: auto">
					<tr>
						<td>
							Filter by name:
						</td>
						<td>
							<input type="text" name="cusName" />
						</td>

						<td>
							<button type="submit" style="width: 120px; height: 25px">
								Filter
							</button>
						</td>
					</tr>
					<tr>
						<td>
							<a class="link" href="/DepartmentManage/Insert_Customer.jsp">Create New</a>
						</td>
					</tr>
				</table>
				</form>
			</div>
			<table
				style="border: 1px; border-color: black; min-width: 400px; margin: 0px auto">
				<tr>
					<td style="background-color: #006699;">
						Full name
					</td>
					<td style="background-color: #006699;">
						Phone
					</td>					
					<td style="background-color: #006699;">
						Birth Date
					</td>
					<td style="background-color: #006699;">
						Address
					</td>
					<td style="background-color: #006699;">
						Apartment
					</td>
					<td style="background-color: #006699;">
						Edit
					</td>
					<td style="background-color: #006699;">
						Delete
					</td>
				</tr>
				<%
				CustomersDAO dao = new CustomersDAO();
				
				List<Customers> list = null;
				if(request.getParameter("cusName") != null) {
					list = dao.findByName(request.getParameter("cusName"));
				} else {
					list = dao.findAll();
				}	
				for(Iterator<Customers> i = list.iterator(); i.hasNext(); ) {
					Customers cus = (Customers)i.next();	
				%>
				<tr>
					<td><%=cus.getFullName() %></td>
					<td><%=cus.getPhone() %></td>					
					<td><%=cus.getBirthday() %></td>
					<td><%=cus.getAddress() %></td>
					<td><%=cus.getApartments().getApartmentNumber()%></td>
					<td>
						<a class="link"
							href="/DepartmentManage/Update_Customers.jsp?id=<%=cus.getId()%>">Edit</a>
					</td>
					<td>
						<a class="link" href="javascript:doDelete(<%=cus.getId()%>)">Delete</a>
					</td>
				</tr>
				<% 	} %>
			</table>
			<script type="text/javascript">
	function doDelete(id)
	{
		input_box=confirm("Bạn có thực sự muốn xóa mẫu tin này không?");
		if (input_box==true) {
		  location.href ="./servlet/CUSTOMER_Servlet?action=delete&id="+id;
		}	
	}
</script>
		</div>
	
	</body>
</html>